package com.cndw.kungfu.model.sys;

import java.util.Collection;

import com.cndw.kungfu.comm.AccessInspection;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsChat;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.domain.Union;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.battle.Team;
import com.cndw.kungfu.vo.ChatVO;

/**
 * 聊天消息封装
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-6-20 下午03:23:38
 */
public class ChatMain {
	
	
	
	public static boolean sendMessage(int type,String message,Player sendPlayer){
		return sendMessage(type, message, sendPlayer, false);
	}
	/**
	 * 只支持用户发的基本类型聊天.若修改.同步修改用户入口验证
	 * 
	 * @param type
	 * @param message
	 * @param sendPlayer
	 * @return
	 */
	public static boolean sendMessage(int type, String message, Player sendPlayer,boolean hidden) {
		
		ChatVO chat = new ChatVO();
		chat.setType(type);
		chat.setMessage(message);
		chat.setPlayerId(sendPlayer.getPlayerId());
		chat.setPlayerName(sendPlayer.getPlayerName());
		// chat.setVipLevel(sendPlayer.getVipLevel());
		chat.setKnightLevel(sendPlayer.getKnightTrueLevel());

		Scene scene = Context.getSceneServer().getSceneById(sendPlayer.getSceneId());
		if (scene != null) {
			chat.setSceneName(scene.getName());
		}
		if (AccessInspection.isGmInstructor(sendPlayer.getAccountName())) {
			chat.setColorName(1);
		}

		String idKey = null;
		switch (type) {
		case ConstantsChat.Chat_Union:// 帮会聊天
			Union union = Context.getUnionServer().getUnionById(sendPlayer.getUnionId());
			if (union == null) {
				return false;
			}
			union.broadcast(chat, ConstantsRadio.CHAT);
			break;
		case ConstantsChat.Chat_Team:// 队伍聊天
			Team team = sendPlayer.getTeam();
			if (team != null) {
				team.broadcast(chat, ConstantsRadio.CHAT);
			}
			break;
		case ConstantsChat.Chat_World:// 世界
			idKey = Constants.COMMRADIO;
			break;
		case ConstantsChat.Chat_Speaker:// 传音喇叭
			idKey = Constants.COMMRADIO;
			break;
		case ConstantsChat.Chat_Scene:// 场景
			idKey = Context.getSceneServer().getRadioKey(sendPlayer.getSceneId(), sendPlayer.getSubLine());
			break;
		
		default:
			return false;
		}
		if (hidden) {
			Context.getTcpServer().broadcast(chat, ConstantsRadio.CHAT, sendPlayer.getPlayerId());
		}else if(idKey != null) {
			Context.getTcpServer().broadcast(chat, ConstantsRadio.CHAT, idKey);
		}

		return true;
	}

	public static boolean privateMessage(String message, Player sendPlayer, long receiveId) {
		ChatVO chat = new ChatVO();
		chat.setType(ConstantsChat.Chat_Private);
		chat.setMessage(message);
		chat.setPlayerId(sendPlayer.getPlayerId());
		chat.setPlayerName(sendPlayer.getPlayerName());
		chat.setReceiveId(receiveId);
		chat.setVocationId(sendPlayer.getVocationId());
		Context.getTcpServer().broadcast(chat, ConstantsRadio.CHAT, receiveId, sendPlayer.getPlayerId());
		return true;
	}

	/**
	 * 系统消息
	 * 
	 * @param message
	 * @return
	 */
	public static boolean systemMessage(String message) {
		return systemMessageFinal(ConstantsChat.Chat_System, message, 0, 0);
	}

	@Deprecated
	public static boolean systemMessage(String message, int openKey) {
		return systemMessageFinal(ConstantsChat.Chat_System, message, 0, openKey);
	}

	public static boolean commMessage(Player player, String message, int openKey, Object parm) {
		return commMessageFinal(ConstantsChat.Chat_World, message, player, 0, openKey, parm);
	}

	public static boolean hearsayMessage(String message, int openKey) {
		return systemMessageFinal(ConstantsChat.Chat_Hearsay, message, 0, openKey);
	}

	public static boolean hearsayMessage(String message, int openKey, int delay) {
		return systemMessageFinal(ConstantsChat.Chat_Hearsay, message, delay, openKey);
	}

	public static boolean systemMessageDelay(String message, int delay) {
		return systemMessageFinal(ConstantsChat.Chat_System, message, delay, 0);
	}

	public static boolean tipMessage(String message, int color, Collection<Long> ids) {
		ChatVO chat = new ChatVO();
		chat.setType(ConstantsChat.Chat_SysTip);
		chat.setMessage(message);
		chat.setColor(color);
		Context.getTcpServer().broadcast(chat, ConstantsRadio.CHAT, ids);
		return true;
	}

	private static boolean systemMessageFinal(int type, String message, int delay, int openKey) {
		ChatVO chat = new ChatVO();
		chat.setType(type);
		chat.setMessage(message);
		chat.setOpenKey(openKey);
		String idKey = Constants.COMMRADIO;
		Context.getTcpServer().broadcast(chat, ConstantsRadio.CHAT, idKey, delay);

		return true;
	}

	private static boolean commMessageFinal(int type, String message, Player sendPlayer, int delay, int openKey, Object parm) {
		ChatVO chat = new ChatVO();
		chat.setType(type);
		chat.setMessage(message);
		chat.setOpenKey(openKey);
		if (parm != null) {
			chat.setParm(parm);
		}
		if (sendPlayer != null) {
			chat.setPlayerId(sendPlayer.getPlayerId());
			chat.setPlayerName(sendPlayer.getPlayerName());
			chat.setKnightLevel(sendPlayer.getKnightTrueLevel());
			Scene scene = Context.getSceneServer().getSceneById(sendPlayer.getSceneId());
			if (scene != null) {
				chat.setSceneName(scene.getName());
			}
		}
		String idKey = Constants.COMMRADIO;
		Context.getTcpServer().broadcast(chat, ConstantsRadio.CHAT, idKey, delay);

		return true;
	}
	/**
	 * 系统发布消息
	 * @param message
	 * @param openKey
	 * @param delay
	 * @return
	 */
	public static boolean speakerMessage(String message,int openKey,int delay){
		ChatVO chat = new ChatVO();
		chat.setType(ConstantsChat.Chat_Speaker);
		chat.setMessage(message);
		chat.setOpenKey(openKey);
		Context.getTcpServer().broadcast(chat, ConstantsRadio.CHAT, Constants.COMMRADIO, delay);
		return true;
	}

}
